import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  Unique,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

import { DietLog, Food } from '@/modules/app/entities';
import { IsNumber, IsOptional, IsString } from 'class-validator';

@Entity('diet_food')
@Unique(['dietLogId', 'foodId'])
export class DietFood {
  @PrimaryGeneratedColumn('increment')
  dietFoodId: number;

  @Column()
  dietLogId: number;

  @ManyToOne(() => DietLog, (dietLog) => dietLog.dietFoods)
  @JoinColumn({ name: 'dietLogId' })
  dietLog: DietLog;

  @Column()
  foodId: number;

  @ManyToOne(() => Food, (food) => food.dietFoods)
  @JoinColumn({ name: 'foodId' })
  food: Food;

  /**
   * 食用量,多少份
   */
  @Column({
    type: 'float',
    default: 0,
  })
  count: number;

  /**
   * 食用量,多少克
   */
  @Column({
    type: 'float',
    default: 0,
  })
  quantity: number;

  /**
   * 单位
   * @example 盘
   * @example 份
   */
  @IsOptional()
  @IsString()
  @Column()
  unit?: string;

  /**
   * 每单位食物的克数
   * @example 0.5
   * @example 100
   */
  @IsOptional()
  @IsNumber()
  @Column({ type: 'float' })
  unitWeight?: number = 0;

  @CreateDateColumn()
  createdAt?: Date;

  @UpdateDateColumn()
  updatedAt?: Date;
}
